﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace LegtionMVVM
{
    /// <summary>
    ///管理字段如何绑定
    /// </summary>
    internal class BindingManager
    {
        public static void BindData<TView,TViewModel>(List<ToBindControl> bindControlList,TView view,TViewModel viewModel)
            where TView : Form
            where TViewModel : NotifyPropertyChanged
        {            
            if (bindControlList != null && bindControlList.Count > 0)
            {
                BindingProperty<TView,TViewModel> bindingProperty = new BindingProperty<TView,TViewModel>();
                foreach (ToBindControl item in bindControlList)
                {
                    bindingProperty.SetPropertyBinding(item,view,viewModel);
                }
            }
        }
    }

    internal class BindingProperty<TView, TViewModel>
        where TView : Form
        where TViewModel : NotifyPropertyChanged
    {
        public void SetPropertyBinding(ToBindControl bindControl, TView view, TViewModel viewModel)
        {
            if (bindControl == null||bindControl.BindingSourceList.Count==0)
                return;
            foreach (KeyValuePair<string,BindSource> bindSource in bindControl.BindingSourceList)
            {
                if(bindSource.Value.BindingMode== BindingMode.TwoWay||
                    bindSource.Value.BindingMode== BindingMode.OneWayC2D)
                    SetPropertyBindingOneWayC2D(bindSource.Key, bindSource.Value,view,viewModel);

                if (bindSource.Value.BindingMode == BindingMode.TwoWay ||
             bindSource.Value.BindingMode == BindingMode.OneWayD2C)
                    SetPropertyBindingOneWayD2C(bindSource.Key, bindSource.Value, bindControl,view,viewModel);
            }
        }
        private void SetPropertyBindingOneWayC2D(string propertyKey, BindSource bindSource, TView view, TViewModel viewModel)
        {
        }
        private void SetPropertyBindingOneWayD2C(string propertyKey, BindSource bindSource, ToBindControl bindControl, TView view, TViewModel viewModel)
        {
            DelegatedReflection drf = new DelegatedReflection();
            int layerIndex = bindSource.LayerSourcePath.Count;
            object lastObjValue = viewModel;
            foreach (KeyValuePair<string,TypeSource> item in bindSource.LayerSourcePath)
            {
                if (!string.IsNullOrEmpty(item.Key))
                    lastObjValue = drf.GetValue(lastObjValue, item.Key);
            }
            if (lastObjValue!=null&&!string.IsNullOrEmpty(bindSource.PropertyKey))
                lastObjValue = drf.GetValue(lastObjValue, bindSource.PropertyKey);

            if (lastObjValue != null && !string.IsNullOrEmpty(bindSource.PropertyKey) && !string.IsNullOrEmpty(propertyKey))
            {
                if ((bindSource.DesProperty.PropertyType.Name == "String" || !bindSource.DesProperty.PropertyType.IsClass)
                    && (bindSource.SrcProperty.PropertyType.Name == "String" || !bindSource.SrcProperty.PropertyType.IsClass))
                {
                    lastObjValue = ConverData.ToConver(bindSource.SrcProperty.PropertyType, bindSource.DesProperty.PropertyType, lastObjValue);
                }
                drf.SetValue(bindControl.Component, propertyKey, lastObjValue);
            }

 
        }
 
    }
}
